import java.sql.*;
import java.util.Scanner;

public class Main {
    public static void main(String arg[]) {
       // m1();
       // m2();
        //m3();
        m4();
    }

    private static void m1() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        //1.加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");

            // 2.获取连接
            String url = "jdbc:mysql://127.0.0.1:3306/lib_dtb?characterEncoding=utf-8";
            // .var
            connection = DriverManager.getConnection(url, "root", "3141");
//            if (connection != null) {
//                System.out.println("连接成功");
//            }
            // 3.写sql
            String sql = "select * from admin";

            // 4.预编译sql
            preparedStatement = connection.prepareStatement(sql);

            // 5.设置参数
            //键盘录入

            // 5.执行sql
            resultSet = preparedStatement.executeQuery();

            // 6.遍历结果集
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String username = resultSet.getString("username");
                String password = resultSet.getString("password");
                System.out.println(id + "----" + username + "----" + password);
            }


        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }//finally不论程序是否异常都执行
        finally {// 7.关闭资源
            try {
                if (resultSet != null) {
                    resultSet.close();
                }

                if (preparedStatement != null) {
                    preparedStatement.close();
                }

                if (connection != null) {
                    connection.close();
                }

            } catch (SQLException e) {
                throw new RuntimeException(e);
            }


        }
    }

    private static void m2() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        //1.加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");

            // 2.获取连接
            String url = "jdbc:mysql://127.0.0.1:3306/lib_dtb?characterEncoding=utf-8";
            // .var
            connection = DriverManager.getConnection(url, "root", "3141");
//            if (connection != null) {
//                System.out.println("连接成功");
//            }
            // 3.写sql
            String sql = "select * from admin where username like ?";


            // 4.预编译sql
            preparedStatement = connection.prepareStatement(sql);

            //设置参数
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入用户名：");
            String username = scanner.nextLine();
            preparedStatement.setString(1,"%"+username+"%");


            // 5.执行sql
            resultSet = preparedStatement.executeQuery();

            // 6.遍历结果集
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("username");
                String password = resultSet.getString("password");
                System.out.println(id + "----" + name + "----" + password);
            }


        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }//finally不论程序是否异常都执行
        finally {// 7.关闭资源
            try {
                if (resultSet != null) {
                    resultSet.close();
                }

                if (preparedStatement != null) {
                    preparedStatement.close();
                }

                if (connection != null) {
                    connection.close();
                }

            } catch (SQLException e) {
                throw new RuntimeException(e);
            }


        }


    }

    private static void m3() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;

        //1.加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");

            // 2.获取连接
            String url = "jdbc:mysql://127.0.0.1:3306/lib_dtb?characterEncoding=utf-8";
            // .var
            connection = DriverManager.getConnection(url, "root", "3141");
//            if (connection != null) {
//                System.out.println("连接成功");
//            }
            // 3.写sql
            String sql = "insert into  admin (username,password) values(?,?)";

            // 4.预编译sql
            preparedStatement = connection.prepareStatement(sql);

            //设置参数
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入用户名：");
            String username = scanner.nextLine();
            preparedStatement.setString(1,username);
            System.out.println("请输入密码：");
            String password = scanner.nextLine();
            preparedStatement.setString(2,password);
            System.out.println(username+"----"+password);
            System.out.println(preparedStatement);



            // 5.执行sql
            int result = preparedStatement.executeUpdate();

            if(result>0){
                System.out.println("添加成功");
            }else{
                System.out.println("添加失败");
            }


        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }//finally不论程序是否异常都执行
        finally {// 7.关闭资源
            try {


                if (preparedStatement != null) {
                    preparedStatement.close();
                }

                if (connection != null) {
                    connection.close();
                }

            } catch (SQLException e) {
                throw new RuntimeException(e);
            }


        }
    }

    private static void m4() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        //1.加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");

            // 2.获取连接
            String url = "jdbc:mysql://127.0.0.1:3306/lib_dtb?characterEncoding=utf-8";
            // .var
            connection = DriverManager.getConnection(url, "root", "3141");
//            if (connection != null) {
//                System.out.println("连接成功");
//            }
            // 3.写sql
            String sql = "update admin set username =? password=?  where id = 2;";

            // 4.预编译sql
            preparedStatement = connection.prepareStatement(sql);

            //设置参数
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入用户名：");
            String username = scanner.nextLine();
            preparedStatement.setString(1,username);
            System.out.println("请输入密码：");
            String password = scanner.nextLine();

            preparedStatement.setString(2,password);
            System.out.println(username+"----"+password);
            System.out.println(preparedStatement);
            preparedStatement.executeUpdate();
            System.out.println("修改成功");
            return;



          

            // 6.遍历结果集


        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }//finally不论程序是否异常都执行
        finally {// 7.关闭资源
            try {
                if (resultSet != null) {
                    resultSet.close();
                }

                if (preparedStatement != null) {
                    preparedStatement.close();
                }

                if (connection != null) {
                    connection.close();
                }

            } catch (SQLException e) {
                throw new RuntimeException(e);
            }


        }
    }












}

